#ifndef ARDUINO_ARCH_ESP32
#warning Not ESP32
#else
#ifndef ML_ESP32
#define ML_ESP32 TRUE

#include "esp32-hal-ledc.h"
/**
 * D2 ADC12 2
 * D3 ADC11 0
 * D4 ADC10 4
 * D5 ADC18 25
 * D6 ADC19 26
 * D7 ADC17 27
 * A0 ADC0 36
 * A1 ADC3 39
 * A2 ADC16 14
 * A3 ADC15 12
 * A4 ADC14 13
 * A5 ADC13 15
 */
uint8_t getChannel(uint8_t analogOutPin){
    uint8_t channel = 0;
    if(analogOutPin == 2){
        channel = 1;
    }else if(analogOutPin == 0){
        channel = 2;
    }else if(analogOutPin == 4){
        channel = 3;
    }else if(analogOutPin == 25){
        channel = 4;
    }else if(analogOutPin == 26){
        channel = 5;
    }else if(analogOutPin == 27){
        channel = 6;
    }else if(analogOutPin == 36){
        channel = 7;
    }else if(analogOutPin == 39){
        channel = 8;
    }else if(analogOutPin == 14){
        channel = 9;
    }else if(analogOutPin == 12){
        channel = 10;
    }else if(analogOutPin == 13){
        channel = 11;
    }else if(analogOutPin == 15){
        channel = 12;
    }
    return channel;
}
void analogWrite(uint8_t _pin, int outputValue){
    uint8_t channel = getChannel(_pin);
    ledcAttachPin(_pin, channel); // assign pins to channels
    ledcSetup(channel, 12000, 8);
    ledcWrite(channel, outputValue);
}

void tone(uint8_t _pin, unsigned int frequency, unsigned long duration = 0){
    uint8_t channel = getChannel(_pin);
    ledcAttachPin(_pin, channel); // assign pins to channels
    ledcSetup(channel, 12000, 8);
    ledcWriteTone(channel, frequency);
}
void noTone(uint8_t _pin){
    uint8_t channel = getChannel(_pin);
    ledcAttachPin(_pin, channel); // assign pins to channels
    ledcSetup(channel, 12000, 8);
    ledcWriteTone(channel, 0);
}

#endif
#endif